/**
 * 
 */
package net.xfok;

import java.util.Date;
import java.util.List;
import java.util.Random;

import net.xfok.dao.PeopleDAO;
import net.xfok.entity.People;
import android.app.Activity;
import android.database.SQLException;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

/**
 * @author howard
 * 
 */
public class Demo extends Activity {
	private PeopleDAO peopleDAO;
	TextView txtMsg;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.demo);
		peopleDAO=new PeopleDAO(this);
		peopleDAO.printQuery(true);
		txtMsg = (TextView) this.findViewById(R.id.txtMsg);
		Button btn1 = (Button) this.findViewById(R.id.btn1);
		btn1.setText("创建表");
		btn1.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				CreateTable();
			}
		});

		Button btn2 = (Button) this.findViewById(R.id.btn2);
		btn2.setText("插入 3 条记录");
		btn2.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				insertItem();
			}
		});

		Button btn3 = (Button) this.findViewById(R.id.btn3);
		btn3.setText("删除全部记录");
		btn3.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				deleteItem();
			}
		});

		Button btn4 = (Button) this.findViewById(R.id.btn4);
		btn4.setText("更新指定数据");
		btn4.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				updateItem();
			}
		});

		Button btn5 = (Button) this.findViewById(R.id.btn5);
		btn5.setText("显示全部数据");
		btn5.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				showItems();
			}
		});

		Button btn6 = (Button) this.findViewById(R.id.btn6);
		btn6.setText("删除表");
		btn6.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				dropTable();
			}
		});
	}

	// 创建数据表
	private void CreateTable() {
		if(peopleDAO.createTable()){
			txtMsg.append("数据表成功创建\n");
		}else{
			txtMsg.append("数据表创建错误\n");
		}
	}

	// 插入数据
	private void insertItem() {		
		try {
			Random random = new Random();
			for (int i = 0; i < 3; i++) {
				People people=new People();
				people.setBirth(new Date());
				people.setHight(random.nextInt(200));
				people.setName("Howard"+i);
				people.setSex(0);
				people.setWeight(12.2f);
				peopleDAO.create(people);
			}
			txtMsg.append("成功插入 3 条数据\n");
		} catch (SQLException ex) {
			txtMsg.append("插入数据失败\n" + ex.toString() + "\n");
		}
	}

	// 删除数据
	private void deleteItem() {
		try {
			String where="id<9999";
			peopleDAO.delete(where);
			txtMsg.append("成功删除数据\n");
		} catch (SQLException e) {
			txtMsg.append("删除数据失败\n");
		}
	}

	// 更新数据
	private void updateItem() {
		try {
			People people=(People)peopleDAO.select(2);
			people.setName("霍华德");
			peopleDAO.update(people);
			txtMsg.append("成功更新数据\n");
		} catch (SQLException e) {
			txtMsg.append("更新数据失败\n");
		}
	}

	// 查询数据
	private void showItems() {
		try {
//			txtMsg.append("共 " + peopleDAO.count() + " 条记录\n");
//			Cursor cursor=peopleDAO.select();
//			while (cursor.moveToNext()) {			
//				txtMsg.append(cursor.getInt(cursor.getColumnIndex("id"))+
//						cursor.getString(cursor.getColumnIndex("name"))+"\n");
//			}
			List<Object> peoples=peopleDAO.selectAll();
			for(int i=0;i<peoples.size();i++){
				People p=(People)peoples.get(i);
				txtMsg.append(p.getName()+" "+p.getId()+" "+p.getBirth()+"\n");
			}
		} catch (SQLException ex) {
			txtMsg.append("读取数据失败\n" + ex.toString() + "\n");
		}
	}

	// 删除数据表
	private void dropTable() {
		if(peopleDAO.dropTable()){
			txtMsg.append("数据表删除成功\n");
		}else{
			txtMsg.append("数据表删除错误\n");
		}
	}
}
